using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MotionBlur.Butler.Triggers;
using MotionBlur.Butler.Runtime;
using System.Threading;

namespace MotionBlur.Butler.Test
{
    
    [TestClass]
    public class ScheduleTests
    {

        public ScheduleTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestHourlyTriggerGetNextOcurrence()
        {

            HourlyTrigger trigger = new HourlyTrigger();

            trigger.StartBy = new DateTime(2000, 1, 1, 0, 0, 0);
            trigger.EndBy = DateTime.MaxValue;

            trigger.RecurEvery = 27;
            
            DateTime now = new DateTime(2000, 1, 2, 0, 0, 0);

            TimeSpan? spanToNext = trigger.GetSpanToNextOccurence(now);

            Assert.IsTrue(spanToNext.HasValue);

            Assert.AreEqual<TimeSpan>(TimeSpan.FromHours(3), spanToNext.Value);

        }


        [TestMethod]
        public void TestHourlyTriggerMonitorCallback()
        {

            ManualResetEvent reset = new ManualResetEvent(false);

            ScheduledMonitor monitor = new ScheduledMonitor();

            Guid staticOrderID = Guid.NewGuid();
            
            HourlyTrigger trigger = new HourlyTrigger();

            trigger.StartBy = new DateTime(2000, 1, 1, 0, 0, 0);
            trigger.EndBy = DateTime.MaxValue;

            trigger.RecurEvery = 0.0166;
            
            DateTime now = new DateTime(2000, 1, 2, 0, 0, 0);

            TriggerCallback callback = delegate(Guid returnedOrderID, Dictionary<string, object> parameters)
            {

                Assert.AreEqual<Guid>(staticOrderID, returnedOrderID);

                reset.Set();

            };

            monitor.StartMonitor();

            monitor.RegisterTrigger(trigger, staticOrderID, callback);

            reset.WaitOne();

        }

    }

}